/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.base.web.card;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.base.entity.card.BaseCard;
import com.thinkgem.jeesite.modules.base.service.card.BaseCardService;

/**
 * 基础管理_借阅证信息生成Controller
 * @author 呆子
 * @version 2020-02-22
 */
@Controller
@RequestMapping(value = "${adminPath}/base/card/baseCard")
public class BaseCardController extends BaseController {

	@Autowired
	private BaseCardService baseCardService;
	
	@ModelAttribute
	public BaseCard get(@RequestParam(required=false) String id) {
		BaseCard entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = baseCardService.get(id);
		}
		if (entity == null){
			entity = new BaseCard();
		}
		return entity;
	}
	
	@RequiresPermissions("base:card:baseCard:view")
	@RequestMapping(value = {"list", ""})
	public String list(BaseCard baseCard, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BaseCard> page = baseCardService.findPage(new Page<BaseCard>(request, response), baseCard); 
		model.addAttribute("page", page);
		return "modules/base/card/baseCardList";
	}

    @RequestMapping(value = "selectCardList")
    public String selectCardList(BaseCard baseCard, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<BaseCard> page = baseCardService.selectCardPage(new Page<BaseCard>(request, response), baseCard);
        model.addAttribute("page", page);
        return "modules/base/card/selectCardList";
    }

	@RequiresPermissions("base:card:baseCard:view")
	@RequestMapping(value = "form")
	public String form(BaseCard baseCard, Model model) {
		model.addAttribute("baseCard", baseCard);
		return "modules/base/card/baseCardForm";
	}

	@RequiresPermissions("base:card:baseCard:edit")
	@RequestMapping(value = "save")
	public String save(BaseCard baseCard, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, baseCard)){
			return form(baseCard, model);
		}
		baseCardService.save(baseCard);
		addMessage(redirectAttributes, "保存基础管理_借阅证成功");
		return "redirect:"+Global.getAdminPath()+"/base/card/baseCard/?repage";
	}
	
	@RequiresPermissions("base:card:baseCard:edit")
	@RequestMapping(value = "delete")
	public String delete(BaseCard baseCard, RedirectAttributes redirectAttributes) {
		baseCardService.delete(baseCard);
		addMessage(redirectAttributes, "删除基础管理_借阅证成功");
		return "redirect:"+Global.getAdminPath()+"/base/card/baseCard/?repage";
	}

}